package com.realgodo.site.web.user.space.notice;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springside.modules.orm.Page;

import com.realgodo.site.entity.space.SpaceNotice;
import com.realgodo.site.service.space.SpaceNoticeManager;
import com.realgodo.site.web.user.UserDefaultController;

@Scope("prototype")
@Results( { @Result(name = "toindex", type = "redirect", location = "${base}/user/space/notice/") })
public class IndexController extends UserDefaultController {

	private static final long serialVersionUID = 2823326229491511113L;

	@Autowired
	private SpaceNoticeManager spaceNoticeManager;

	private Page<SpaceNotice> noticePage = new Page<SpaceNotice>(PAGE_SIZE);

	private SpaceNotice spaceNotice;

	public String execute() {
		noticePage.setPageNo(spaceNoticeManager.isParamNotNull(getParamMap(),
				Page.PAGE) ? Integer.valueOf((String) spaceNoticeManager
				.getParamValue(getParamMap(), Page.PAGE)) : 1);// 查询页数
		this.noticePage = this.spaceNoticeManager.findSpaceNotices(noticePage,
				super.getMySpace().getId());
		super.setPageInfo(noticePage);
		return SUCCESS;
	}

	public String add() {
		if (getId() != null) {
			spaceNotice = this.spaceNoticeManager.get(getId());
		}
		return "add";
	}

	public String save() {
		if (spaceNotice != null) {
			spaceNotice.setSpace(getMySpace());
			this.spaceNoticeManager.save(spaceNotice);
		}

		return "toindex";
	}

	public String remove() {
		if (getId() != null) {
			this.spaceNoticeManager.delete(getId());
		}
		return "toindex";
	}

	/**************** getter and setter *********************/
	public Page<SpaceNotice> getNoticePage() {
		return noticePage;
	}

	public void setNoticePage(Page<SpaceNotice> noticePage) {
		this.noticePage = noticePage;
	}

	public SpaceNotice getSpaceNotice() {
		return spaceNotice;
	}

	public void setSpaceNotice(SpaceNotice spaceNotice) {
		this.spaceNotice = spaceNotice;
	}

}
